<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="事件名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="事件名称"></el-input>
    </el-form-item>
    <el-form-item label="事件类型" prop="type">
      <el-select v-model="dataForm.type" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
        v-model="dataForm.startTime"
        type="date"
        placeholder="选择日期"
        format="yyyy 年 MM 月 dd 日"
        value-format="timestamp">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
        v-model="dataForm.endTime"
        type="date"
        placeholder="选择日期"
        format="yyyy 年 MM 月 dd 日"
        value-format="timestamp">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="状态" prop="state">
       <el-radio v-model="dataForm.state" :label="1">启用</el-radio>
       <el-radio v-model="dataForm.state" :label="2">禁用</el-radio>
     </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        options:[
          {
            label:"会员注册",
            value:1
          },
          {
            label:"会员生日",
            value:2
          },
          {
            label:"会员升级",
            value:3
          },
          {
            label:"会员充值",
            value:4
          },
          {
            label:"已完成",
            value:5
          },
          {
            label:"五星好评",
            value:6
          },
        ],
        dataForm: {
          id: 0,
          name: '',
          type: '',
          sellerId: '',
          startTime: '',
          endTime: '',
          state: 1
        },
        dataRule: {
          name: [
            { required: true, message: '事件名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '门店ID不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态 1启用2禁用不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelcouponsevent/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tHotelCouponsEvent.name
                this.dataForm.type = data.tHotelCouponsEvent.type
                this.dataForm.sellerId = data.tHotelCouponsEvent.sellerId
                this.dataForm.startTime = data.tHotelCouponsEvent.startTime
                this.dataForm.endTime = data.tHotelCouponsEvent.endTime
                this.dataForm.state = data.tHotelCouponsEvent.state
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelcouponsevent/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'type': this.dataForm.type,
                'sellerId': this.dataForm.sellerId,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
